"""共享变量+锁"""
# encoding=utf-8
import time
from multiprocessing import Process, Value, Lock


class Counter(object):
    def __init__(self, initval=0):
        self.val = Value('i', initval)
        self.lock = Lock()

    def increment(self):
        with self.lock:
            self.val.value += 1  # 共享变量自加1
            # print("increment one time!",self.value() )
            # 加此句死锁

    def value(self):
        with self.lock:
            return self.val.value


def func(counter):
    for i in range(50):
        time.sleep(0.01)
        counter.increment()


if __name__ == '__main__':
    counter = Counter(0)
    procs = [Process(target=func, args=(counter,)) for i in range(10)]
    # 等价于
    # for i in range(10):
    # Process(target = func, args = (counter,))
    for p in procs: p.start()
    for p in procs: p.join()
    print(counter.value())
